<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import ObserveDOM from '@better-scroll/observe-dom'  //对2.0的兼容
BScroll.use(ObserveDOM)

export default {
  name: "Scroll",
  props: {
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      scroll: null,
      // message: '哈哈哈'
    }
  },
  // 2.0版装两个插件observe-dom observe-image 启用就好了
  // 2.0不能滚的 
  // 要先安装observe-dom插件，然后再开启observeDOM: true
  mounted() {
  // updated() {
    // this.scroll = new BScroll(document.querySelector('wrapper'), {})
    
    // 1.创建BScroll对象
    this.scroll = new BScroll(this.$refs.wrapper, {
      probeType: this.probeType, 
      pullUpLoad: this.pullUpLoad, 
      click: true,     //用于div元素是否可以点击
      // mouseWheel: true, 
      observeImage: true,
      observeDOM: true
    })

    console.log('scroll:', this.scroll);

    // 2.监听滚动的位置
    if (this.probeType ===2 || this.probeType === 3) {
      this.scroll.on('scroll', (position) => {
        // console.log(position);
        this.$emit('scroll', position)

        this.scroll.refresh()
      })
    }

    // 3.监听scroll滚动到底部
    if (this.pullUpLoad) {
      this.scroll.on('pullingUp', (position) => {
        this.$emit('pullingUp', position)
      })
    }

  },
  methods: {
    scrollTo(x, y, time=300) {
      this.scroll && this.scroll.scrollTo && this.scroll.scrollTo(x, y, time)
    },
    finishPullUp() {
      this.scroll && this.scroll.finishPullUp && this.scroll.finishPullUp()
    },
    refresh() {
      console.log('scroll----');
      this.scroll && this.scroll.refresh && this.scroll.refresh()
    },
    getScrollY() {
      return this.scroll ? this.scroll.y : 0
    }
  }
}
</script>

<style scoped>

</style>